﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrmHotel
{
    public partial class FrmShangPinTiudan : Form
    {
        public FrmShangPinTiudan()
        {
            InitializeComponent();
        }
        int shangpinshu;//接收商品剩余的数量
        public int Sub;//接收商品编号

        public int fanghao;//接收房间号
        private void FrmShangPinTiudan_Load(object sender, EventArgs e)
        {
            //string sql = "select SsooSum from Consume where CsoId=" + Sub;
            string sql = String.Format("select SsooSum from Consume c inner join  Commodity a on c.SsoName=a.CyId where a.CyId={0} and c.CsoRoom={1}", Sub, fanghao);
            shangpinshu = Convert.ToInt32(DBHelper.SelectOne(sql)); 
            this.label4.Text = "可退货" + shangpinshu + "个商品";

            string sql1 = "select CyName from Commodity where CyId=" + Sub;
            SqlDataReader r = DBHelper.SelectMore(sql1);
            while (r.Read())
            {
                this.textBox1.Text = r["CyName"].ToString();
            }
            r.Close();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int danjia = 0;
            if (numericUpDown1.Value > shangpinshu)
            {
                MessageBox.Show("您退的商品不可以大过购买的数量");
                return;
            }

            else if (numericUpDown1.Value == shangpinshu)
            {
                string sql = string.Format("delete  Consume where SsoName={0} and CsoRoom={1}", Sub, fanghao);
                int hang = DBHelper.ExecuteUpdate(sql);
                string sql1 = string.Format("delete  TongJioo where SsoName={0} and CsoRoom={1}", Sub, fanghao);
                int hang1 = DBHelper.ExecuteUpdate(sql1);
                if (hang > 0)
                {
                    MessageBox.Show("退单成功");
                    this.DialogResult = DialogResult.OK;
                    return;
                }
            }
            else
            {
                string sql1 = "select SsoName from Consume where SsoName=" + Sub;
                int zhi = Convert.ToInt32(DBHelper.SelectOne(sql1));
                if (zhi > 0)
                {
                    string fuzhi = "select * from Commodity where CyId=" + zhi;
                    SqlDataReader read = DBHelper.SelectMore(fuzhi);
                    if (read.Read())
                    {
                        danjia = Convert.ToInt32(read["CyPrice"]);//商品单价                
                    }
                    read.Close();
                    string xiugai = string.Format("update Consume set SsooSum-={0},CsPrice-={1}*{2}  where SsoName={3} and CsoRoom={4} ", numericUpDown1.Value, danjia, numericUpDown1.Value, Sub,fanghao);
                    int hang1 = DBHelper.ExecuteUpdate(xiugai);
                    string xiugai1 = string.Format("update TongJioo set SsooSum-={0},CsPrice-={1}*{2}  where SsoName={3} and CsoRoom={4} ", numericUpDown1.Value, danjia, numericUpDown1.Value, Sub, fanghao);
                    int hang11 = DBHelper.ExecuteUpdate(xiugai1);
                    if (hang1 > 0)
                    {
                        MessageBox.Show("退单成功");
                        this.DialogResult = DialogResult.OK;
                        return;
                    }
                    else
                    {
                        MessageBox.Show("退单失败");
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
